﻿using System.IO;
using System.Linq;

namespace WhhgCssToMainMenuIcon
{
   /// <summary>
   /// This application take the web hosting hub glyph css file and
   /// converts it to the MainMenuIcon enumeration and switch statement.
   /// </summary>
   class Program
   {
      static void Main (string[] args)
      {
         string input = "..\\..\\whhg.css";
         string enumOutput = "..\\..\\MainMenuEnum.txt";
         string switchOutput = "..\\..\\MainMenuSwitch.txt";

         for (int i = 0; i < args.Length; i++)
         {
            var arg = args [i];
            if (arg == "-f")
               input = args [++i];
            if (arg == "-eo")
               enumOutput = args[++i];
            if (arg == "-so")
               switchOutput = args[++i];
         }

         if (!File.Exists (input))
            throw new FileNotFoundException("Unable to find the required .css file!");
         if (File.Exists (enumOutput))
            File.Delete (enumOutput);
         if (File.Exists (switchOutput))
            File.Delete (switchOutput);

         using (var reader = new StreamReader (input))
         {
            var enumWriter = new StreamWriter (enumOutput);
            var switchWriter = new StreamWriter (switchOutput);

            while (reader.Peek () != -1)
            {
               var line = reader.ReadLine ();
               if (!string.IsNullOrEmpty(line) && line.StartsWith (".icon-"))
               {
                  var lineData = line.Split ('\'');
                  var colonIndex = line.IndexOf (':');
                  var iconName = line.Substring (6, colonIndex - 6);
                  iconName = Capitalize (iconName);
                  if (iconName.Contains ("-"))
                  {
                     var tempName = string.Empty;
                     var nameData = iconName.Split ('-');
                     iconName = nameData.Aggregate (tempName, (current, s) => current + Capitalize (s));
                  }

                  var iconCode = lineData [1].Substring (1);
                  if (iconCode.Length == 2)
                     iconCode = "\\u00" + iconCode;
                  else
                     iconCode = "\\U0000" + iconCode;
                  enumWriter.WriteLine (iconName + ",");
                  switchWriter.WriteLine ("case MainMenuIcon." + iconName + ":");
                  switchWriter.WriteLine ("\treturn  \"" + iconCode + "\";");
               }
            }

            switchWriter.WriteLine ("default:");
            switchWriter.WriteLine ("\treturn String.Empty;");

            enumWriter.Close ();
            switchWriter.Close ();
         }
      }

      private static string Capitalize (string iconName)
      {
         return char.ToUpper (iconName [0]) + iconName.Substring (1);
      }
   }
}
